# 构建 uniapp Android 应用
FROM alvrme/alpine-android:android-35-jdk17

RUN apk add --no-cache git openssh jq xmlstarlet curl tar xz

# 安装 Node.js
ENV NODE_VERSION=20.17.0
RUN curl -fsSLO --compressed "http://192.168.1.3:9010/resource/node-v${NODE_VERSION}-linux-x64.tar.xz" \
    && tar -xJf "node-v${NODE_VERSION}-linux-x64.tar.xz" -C /usr/local --strip-components=1 \
    && rm "node-v${NODE_VERSION}-linux-x64.tar.xz"

RUN mkdir -p /root/.ssh && \
    chmod 700 /root/.ssh && \
    printf "Host *\n\tStrictHostKeyChecking no\n\tUserKnownHostsFile=/dev/null\n" > /root/.ssh/config

COPY ./android/id_rsa /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa

# 克隆 Android 项目
WORKDIR /workspace/android
RUN  git clone --branch master ssh://git@192.168.1.3:2224/iot/androidplugingroup.git .

# 预先下载 Gradle 依赖
COPY ./android/gradle/build-root.gradle ./build.gradle
COPY ./android/gradle/build.gradle ./app/build.gradle

# 修改 gradle-wrapper.properties
RUN sed -i 's|distributionUrl=https\\://services.gradle.org/distributions/gradle-\([0-9.]*\)-bin.zip|distributionUrl=https\\://mirrors.cloud.tencent.com/gradle/gradle-\1-bin.zip|g' ./gradle/wrapper/gradle-wrapper.properties

# 下载 Gradle 依赖
RUN chmod +x ./gradlew && \
    ./gradlew --no-daemon dependencies || true

# 安装 minio 客户端
RUN wget http://192.168.1.3:9010/resource/mc -O /usr/local/bin/mc && \
    chmod +x /usr/local/bin/mc


WORKDIR /workspace/uniapp

COPY ./package.json ./

# 设置镜像源并安装依赖
RUN npm install -g yarn

RUN yarn config set registry https://registry.npmmirror.com/

RUN yarn install

# 复制项目文件
COPY . .

RUN chmod +x ./android/app.sh

# 执行 app.sh 脚本
CMD ["sh", "-c", "./android/app.sh && tail -f /dev/null"]